package main

/*
二叉树的所有路径
给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
*/

import (
	"fmt"
	"strconv"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func binaryTreePaths(root *TreeNode) []string {
	if root == nil {
		return nil
	}
	var paths []string
	var dfs func(*TreeNode, string)
	dfs = func(node *TreeNode, path string) {
		if node != nil {
			path += strconv.Itoa(node.Val)
			if node.Left == nil && node.Right == nil {
				paths = append(paths, path)
			} else {
				path += "->"
				dfs(node.Left, path)
				dfs(node.Right, path)
			}
		}
	}
	dfs(root, "")
	return paths
}

func main() {
	A := &TreeNode{Val: 1}
	A.Left = &TreeNode{Val: 2}
	A.Right = &TreeNode{Val: 3}
	A.Left.Left = &TreeNode{Val: 4}
	A.Left.Right = &TreeNode{Val: 5}
	A.Right.Left = &TreeNode{Val: 6}
	fmt.Println(binaryTreePaths(A))
}
